package com.stepshop.junit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stepshop.core.thread.SendMsgExecutor;
import com.stepshop.sys.bean.SysMsg;
import com.stepshop.sys.service.SysMsgService;
import com.stepshop.wechat.entity.MsgValueColor;
import com.stepshop.wechat.utils.HttpClientUtil;

public  class JunitSendMsg extends JunitBase{
	private  Logger logger=Logger.getLogger(this.getClass());
	@Resource
	private SysMsgService sysMsgService;
	@Resource
	private ThreadPoolTaskExecutor springExecutor;
	@Test
	public void testInsert(){
		SysMsg msg=new SysMsg();
		String data="";
		String openid="o3QjlwMZhnarbph2zGB-kqrAt3L4";
		Integer templateId=1;
		// 只保存后半截
		String url="http://www.baidu.com";
//		Map<String,MsgValueColor> bodyMap=new HashMap<String,MsgValueColor>();
//		bodyMap.put("orderMoneySum", new MsgValueColor("￥5588.00元","#ff0000"));
//		bodyMap.put("first", new MsgValueColor("您已经支付成功"));
//		bodyMap.put("orderProductName", new MsgValueColor("iphone6s"));
//		bodyMap.put("Remark", new MsgValueColor("订单已经进库,我们会及时安排发货"));
		Map<String,MsgValueColor> bodyMap=new HashMap<String,MsgValueColor>();
		bodyMap.put("first", new MsgValueColor("}感谢您选择神工007，您的订单［"+"EL55555D5D5D5D5"+"]正在分配工人，请稍候。"));
		bodyMap.put("remark", new MsgValueColor("您可以绑定您的手机号至神工007用户版，获取以往订单详情，并可实时关注订单状态。"));
//		List<ServiceProject> projects = serviceProjectHService.queryProjectsByOrderId(order.getId());
		bodyMap.put("keyword1", new MsgValueColor("嘟嘟", "#ff0000"));// 服务项目等
		String besDate="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		besDate=sdf.format(new Date());
		bodyMap.put("keyword2",new MsgValueColor(besDate));//预约时间／点击预约服务时间
		bodyMap.put("keyword3",new MsgValueColor("北京市朝阳区望京广顺北大街33号福码大厦"));//地址
		data=JSON.toJSONString(bodyMap);
		msg.setCreateTime(new Date());
		msg.setStatus(0);
		msg.setOpenid(openid);
		// 只保存后半截
		msg.setUrl(url);
		msg.setTemplateId(templateId);
		msg.setSendData(data);
		sysMsgService.add(msg);
	}
	@Test 
	public void sendMsg(){
		HttpClientUtil utils=new HttpClientUtil();
		String json=utils.doPost("http://wechat-pre.0071515.com/pangu-wechat/dimenCode/getToken.do?valid=iiudihfeqpxkegEl4dkiR4SKd","","UTF-8");
		JSONObject object = JSON.parseObject(json);
		String token=object.getString("token");
		String postUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
		SysMsg msg = sysMsgService.queryById(13l);
		String bodyData = msg.getSendData();
		Map<String,Object> dataMap=new HashMap<String,Object>();
		String url=""+msg.getUrl();
		String openid=msg.getOpenid();
		String templateId="";
		Integer iddd=1;
		if(iddd==1){
			templateId="i2_e1jfRyP1wbcrR-EarOCCKul6c66-acVd-JAVOl04";
		}else if(iddd==2){
			
		}else if(iddd==3){
			
		}else{
			
		}
		JSONObject parseObject = JSON.parseObject(bodyData);
		Object parse = JSON.parse(bodyData);
		dataMap.put("data",JSON.parseObject(bodyData));
		dataMap.put("touser",openid);
		dataMap.put("url",url);
		dataMap.put("template_id",templateId);
		String dataJson=JSON.toJSONString(dataMap);
//		String xml = utils.doPost(postUrl, dataJson, "UTF-8");
//		logger.info("发送状态:"+xml);
	}
	@Test
	public void threadSendMsg(){
		Runnable run=new Runnable() {
			@Override
			public void run() {
				sysMsgService.sendMsg();
			}
		};
		springExecutor.execute(run);
		logger.info("");
	}
	@Test
	public void mainSendMsg(){
		sysMsgService.sendMsg();
	}
	@Test
	public void testInstance(){
		logger.info("["+(null instanceof Object)+"]");
	}
}
